#
# Example from article
#
#   The Django Form Validation Framework on Google App Engine
#   http://code.google.com/intl/pl-PL/appengine/articles/djangoforms.html
#

import cgi
import logging

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

from google.appengine.ext.db import djangoforms

class Item(db.Model):
    name = db.StringProperty()
    quantity = db.IntegerProperty(default=1)
    target_price = db.FloatProperty()
    priority = db.StringProperty(default='Medium',choices=[
      'High', 'Medium', 'Low'])
    entry_time = db.DateTimeProperty(auto_now_add=True)
    added_by = db.UserProperty()
    
class ItemForm(djangoforms.ModelForm):
    class Meta:
        model = Item
        exclude = ['added_by']
        

class MainPage(webapp.RequestHandler):
    def get(self):
        logging.info("MainPage.get")
        self.response.out.write('<html><body>'
                                '<form method="POST" '
                                'action="/">'
                                '<table>')
        # This generates our shopping list form and writes it in the response
        self.response.out.write(ItemForm())
        self.response.out.write('</table>'
                                '<input type="submit">'
                                '</form></body></html>')
                                

    def post(self):
        data = ItemForm(data=self.request.POST)
        if data.is_valid():
            # Save the data, and redirect to the view page
            entity = data.save(commit=False)
            entity.added_by = users.get_current_user()
            entity.put()
            self.redirect('/shopping/items.html')
        else:
            # Reprint the form
            self.response.out.write('<html><body>'
                                    '<form method="POST" '
                                    'action="/">'
                                    '<table>')
            self.response.out.write(data)
            self.response.out.write('</table>'
                                    '<input type="submit">'
                                    '</form></body></html>')
                                    
class ItemPage(webapp.RequestHandler):
    def get(self):
        query = db.GqlQuery("SELECT * FROM Item ORDER BY name")
        for item in query:
            self.response.out.write("%s - Need to buy %d, cost $%0.2f each<br>" %
                                    (item.name, item.quantity, item.target_price))
                                    
def main():
    application = webapp.WSGIApplication(
                                         [('/shopping/', MainPage),
                                          ('/shopping/items.html', ItemPage),
                                          ],
                                         debug=True)
    run_wsgi_app(application)

if __name__=="__main__":
    main()
    
                                    